package com.aopeng.util;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 谢辉
 * @description 注册表单校验工具类
 * @data 2020/5/14
 */
public class VerifyUtils {


    //手机号校验
    public static boolean validateMobilePhone(String in) {
        Pattern pattern = Pattern.compile("^[1]\\d{10}$");
        return pattern.matcher(in).matches();
    }

    //邮箱校验
    public static boolean isEmail(String email) {
        if (null == email || "".equals(email)) {
            return false;
        }
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regEx1);
        Matcher m = p.matcher(email);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    //校验支付密码
    public static boolean validateEmailCode(String code) {
        Pattern pattern = Pattern.compile("\\d{6}");
        return pattern.matcher(code).matches();
    }


    public static boolean getStringType(String str) {
        String txt = str + "";
        Pattern p = Pattern.compile("[0-9]*");
        Matcher m = p.matcher(txt);
        if (m.matches()) {// 输入的是数字
            return true;
        }
        p = Pattern.compile("[a-zA-Z]");
        m = p.matcher(txt);
        return false;
    }

    //校验金额
    public static boolean isNumber(BigDecimal decimal) {
        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){2})?$"); // 判断小数点后2位的数字的正则表达式
        Matcher match = pattern.matcher((CharSequence) decimal);
        return match.matches();
    }

    // 判断字符串中是否仅包含字母数字和汉字[0x4e00,0x9fa5]
    public static boolean isLetterDigitOrChinese(String str) {
        String regex = "^[a-z0-9A-Z\u4e00]+$";
        return str.matches(regex);
    }

    //密码校验
    public static boolean isPassword(String loginPassword){
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
        return loginPassword.matches(regex);
    }

    public static final String PATTERN_DEFAULT = "yyyy-MM-dd HH:mm";

    public static Boolean isDateVail(String date) {
        //用于指定 日期/时间 模式
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(VerifyUtils.PATTERN_DEFAULT);
        boolean flag = true;
        try {
            //Java 8 新添API 用于解析日期和时间
            LocalDateTime.parse(date, dtf);
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

}
